<?php
/**
 * Created by PhpStorm.
 * User: xiaoying
 * Date: 2020/7/8
 * Time: 2:14 PM
 */
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段


$user_id = $_GET['user_id']; #用户的id
$c_type = $_GET['c_type'];#充值类型



// 创建连接
//$conn = new mysqli("localhost","root","Keller19940113","tmsj_database" );

$conn = new mysqli("localhost","root","Keller19940113","tmsj_database" );

// Check connection
if ($conn->connect_error) {
	die("连接失败: " . $conn->connect_error);
}

$result = $conn->query("SELECT * FROM ap_option WHERE id=1");

$admin_config;

if ($result->num_rows > 0) {
	// 输出数据
	$admin_config = $result->fetch_assoc();#获取管理员配置
} else {
	echo "0 结果";
}

$c_time = 0;
$c_time_text = 0;


switch ($c_type) {
	case $admin_config['vip1']://7天体验0.1
		$c_time = 7 * 60 * 60 * 24;
		$c_time_text = 7;
		break;
	case $admin_config['vip2']://一个月6
		$c_time = 30 * 60 * 60 * 24;
		$c_time_text = 30;
		break;
	case $admin_config['vip3']://季度18
		$c_time = 90 * 60 * 60 * 24;
		$c_time_text = 90;
		break;
	case $admin_config['vip4']://半年36
		$c_time = 180 * 60 * 60 * 24;
		$c_time_text = 180;
		break;
	case $admin_config['vip5']: //1年66
		$c_time = 365 * 60 * 60 * 24;
		$c_time_text = 365;
		break;
}


//http://tmsj.taomiwo.top/pay/notify.php?user_id=2&c_type=6
//实际c_type就对应的钱


//echo json_encode($admin_config);
//return;
if ($c_type == $admin_config['vip6']) {//要给她开永久 100
	$c_time_text = "永久";
	$sql = "UPDATE ap_user SET type=1 WHERE id=".$user_id;
	$retval = mysqli_query( $conn, $sql );
	if(! $retval )
	{
		die('无法更新数据: ' . mysqli_error($conn));
	}

	echo "永久会员开通成功!";

	$curr_time = time();


	$sql_log = "INSERT INTO ap_timelog (uid,ctime, cid, money, day, lasttime)"."VALUES"."('1', '$curr_time','$user_id', 0, 'all', 'all')";

	$retval = mysqli_query( $conn, $sql_log );
	if(! $retval )
	{
		die('无法更新数据: ' . mysqli_error($conn));
	}
	echo "日志更新成功!";

} else {//给她按时间充值会员
	if ($c_time != 0) {//充值不等于0
		$result = $conn->query("SELECT lasttime FROM ap_user WHERE id=".$user_id);#先查出这个用户剩余会员时间
		$result_time = $result->fetch_array();
		$s_time = $result_time['lasttime'];

		if ($s_time < time()) {//他的会员时间过期
			$c_time_log = time() + $c_time;
			$sql = "UPDATE ap_user SET lasttime=".$c_time_log." WHERE id=".$user_id;

			$retval = mysqli_query( $conn, $sql );
			if(! $retval )
			{
				die('无法更新数据: ' . mysqli_error($conn));
			}
			echo "会员开通成功!";
		} else {

			$c_time_log = $s_time + $c_time;

			$sql = "UPDATE ap_user SET lasttime=".$c_time_log." WHERE id=".$user_id;
			$retval = mysqli_query( $conn, $sql );
			if(! $retval )
			{
				die('无法更新数据: ' . mysqli_error($conn));
			}

			echo "会员续费成功!";
		}

		$curr_time = time();
		$sql_log = "INSERT INTO ap_timelog (uid,ctime, cid, money, day, lasttime)"."VALUES"."('1', '$curr_time','$user_id', 0, '$c_time_text',  '$c_time_log')";

		$retval = mysqli_query( $conn, $sql_log );
		if(! $retval )
		{
			die('无法更新数据: ' . mysqli_error($conn));
		}
		echo "日志更新成功!";


	}

}


